package alf.mapping.view.activity;

import android.widget.*;

import alf.mapping.dto.maps.Item;
import alf.mapping.view.Env;
import alf.mapping.view.Util;
import alf.mapping.view.activity.R;
import alf.mapping.view.visuals.EditorEngine;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;

public class ItemSelectionActivity extends SimpleAbstractActivity {

	String oldType = null;
	EditorEngine editor;
	Spinner diagramSelector;
	ListView itemSelector;
	String[] diagramNames;
	List<Item> items;
	int oldDiagram = -1;
	int oldItem = -1;
	ItemListAdapter itemListAdapter ;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.item_selection);
		editor = EditorEngine.getInstance();

		diagramNames = editor.getDiagramNames();
		diagramSelector = (Spinner) findViewById(R.id.sp_select_diagram);
		diagramSelector.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, diagramNames));
		diagramSelector.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
				public void onItemSelected(AdapterView<?> parent, View pv, int position, long id) {
					selectDiagram(position);
				}
				public void onNothingSelected(AdapterView<?> p1) {
					// TODO: Implement this method
				}
			});
		itemListAdapter = new ItemListAdapter(this);
		int newdiagram = find(diagramNames, editor.getCurrentRoom().getName());
		diagramSelector.setSelection(newdiagram);

		selectDiagram(newdiagram);


    }

	private int find(String[] texts, String searched) {
		int	idx=0;
		for (String text:texts) {
			if (searched.equals(text)) {
				return idx;
			}
			idx++;
		}
		return -1;
	}

	private void selectDiagram(int position) {
		String newName = diagramNames[position];
	
		if (oldDiagram != position) {
			items = editor.getItemsForDiagram(newName);
			itemListAdapter.setItems(items);
			if (oldDiagram == -1) {
				itemSelector = (ListView) findViewById(R.id.item_list);
				itemSelector.setOnItemClickListener(new ListView.OnItemClickListener(){
						public void onItemClick(AdapterView<?> parent, View pv, int position, long id) {
							selectItem(position);
						}
						public void onNothingSelected(AdapterView<?> p1) {
							// TODO: Implement this me
						}
					});
				itemSelector.setAdapter(itemListAdapter);
			}
			

			oldDiagram = position;
			Util.setText(this, R.id.item_path, diagramNames[position] + "/");
		}
	}

	private void selectItem(int position) {
		Util.setText(this, R.id.item_path, diagramNames[oldDiagram] + "/" + items.get(position).getName());
	}

	public void onOk(View v) {
		Intent i = getIntent();
		i.putExtra(Intent.EXTRA_RETURN_RESULT, Util.getText(this, R.id.item_path));
		setResult(RESULT_OK, i);
		finish();
	}

	class ItemListAdapter extends BaseAdapter {

		List<Item> items = null;
		Context ctx;

		public ItemListAdapter(Context ctx) {
			this.ctx = ctx;
		}

		public void setItems(List<Item> items) {
			this.items = items;
			notifyDataSetChanged();
		}

		public List<Item> getItems() {
			return items;
		}

		public int getCount() {
			return items.size();
		}

		public Object getItem(int p1) {
			return items.get(p1);
		}

		public long getItemId(int p1) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			TextView tView;
			if (convertView == null) {  // if it's not recycled, initialize some attributes
			
				AbsListView.LayoutParams layoutParams = new ListView.LayoutParams(
					ListView.LayoutParams.FILL_PARENT,
					ListView.LayoutParams.WRAP_CONTENT);
				tView = new TextView(ctx,null,android.R.style.TextAppearance_Widget_DropDownItem);
				tView.setLayoutParams(layoutParams);
				
			} else {
				tView = (TextView) convertView;
			}
			Item i = items.get(position);
			String text =  i.getName() + "("+ i.getType()+")";
			tView.setText(text);
			return tView;
		}

	}
}
